package memory;

import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import memory.Controller.*;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.*;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import memory.GUI.*;


/**
 *
 * @author Scott Robinson
 */
public class Controller {
    private final MemoryBoardPanel mbp;
    private final Board b;
    private final ControlPanel c;
    private MainFrame frame;
    private SizePrompt sp;    
    private AudioClip ac [];

    private SquarePanel lastClicked;
    private boolean firstClick;
    private Timer timer;
    private TimerHandler timerHandler;
    
    private GlassPane glass;

    private GameDifficulty difficultySetting;

    public Controller (MemoryBoardPanel mbp, Board b, MainFrame frame, ControlPanel c, SizePrompt sp, GameDifficulty gd) {
        
        
        this.difficultySetting = gd;
        this.mbp = mbp;
        this.b = b;
        this.frame = frame;
        this.c = c;
        this.sp = sp;
        ac = new AudioClip [5];

        firstClick = true;

        timerHandler = new TimerHandler();

        for (SquarePanel ms [] : mbp.getAllSquares()) {
            for (SquarePanel s : ms) {
                s.addMouseListener(new SquareListener(s, this));
            }
        }
        c.updateTime();

        glass = new GlassPane(this.frame.getContentPane());        
        glass.setOpaque(false);
        this.frame.setGlassPane(glass);

        ac [0] = java.applet.Applet.newAudioClip(getClass().getResource("GUI\\sounds\\congrats.wav"));
        ac [1] = java.applet.Applet.newAudioClip(getClass().getResource("GUI\\sounds\\diamond.wav"));
        ac [2] = java.applet.Applet.newAudioClip(getClass().getResource("GUI\\sounds\\echobel2.wav"));
        ac [3] = java.applet.Applet.newAudioClip(getClass().getResource("GUI\\sounds\\orly.wav"));
        ac [4] = java.applet.Applet.newAudioClip(getClass().getResource("GUI\\sounds\\quickey.wav"));
    }

    public void enableGlassPane () {
        glass.setVisible(true);
        glass.repaint();        
    }

    public void disableGlassPane() {
        glass.setVisible(false);
        glass.repaint();        
    }

    public void playSound() {
        switch (new Random().nextInt(5)) {
            case 0:
                ac[0].play();
                break;
            case 1:
                ac[1].play();
                break;
            case 2:
                ac[2].play();
                break;
            case 3:
                ac[3].play();
                break;
            case 4:
                ac[4].play();
                break;
        }
    }
    public void startTimer () {
        timer = new Timer(1000, timerHandler);
        timer.start();
    }

    public boolean isFirstClick() {
        return firstClick;
    }
    
    public void setFirstClick (boolean b) {
        firstClick = b;
    }

    public SquarePanel getLastClicked() {
        return lastClicked;
    }

    public void setLastClicked(SquarePanel lastClicked) {
        this.lastClicked = lastClicked;
    }

    public boolean isGameOver () {
        return b.allMatched();
    }

    public void gameOver() {
        timer.stop();

        Timer t = new Timer(500, new ActionListener() {

            public void actionPerformed(ActionEvent e) {                                
                switch (new Random().nextInt(3)) {
                    case 0:
                        frame.getMbp().setBackground(Color.RED);
                        frame.getControlPanel().setBackground(Color.RED);
                        break;
                    case 1:
                        frame.getMbp().setBackground(Color.BLUE);
                        frame.getControlPanel().setBackground(Color.BLUE);
                        break;
                    case 2:
                        frame.getMbp().setBackground(Color.GREEN);
                        frame.getControlPanel().setBackground(Color.GREEN);
                        break;
                }
            }
        });

        t.start();
        
        new Log(c.getCal(), difficultySetting);

        if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(frame,
                String.format("You Won!  You matched all the squares with a time of %s.\n" +
                "Would you like to play again?", c.timerToString()),
                "Another Try?",
                JOptionPane.YES_NO_OPTION)) {
            frame.setVisible(false);
            sp.getImagePanel().resetLog();
            sp.setVisible(true);
            t.stop();
        }
        else System.exit(0);
    }        
    
    
    public class GlassPane extends JComponent {
        public GlassPane(Container contentPane) {
            CBListener listener = new CBListener(this, contentPane);
            addMouseListener(listener);
            addMouseMotionListener(listener);
        }

        public void itemStateChanged(ItemEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

         public class CBListener extends MouseInputAdapter {
            GlassPane glassPane;
            Container contentPane;

            public CBListener(GlassPane glassPane, Container contentPane) {
                this.glassPane = glassPane;
                this.contentPane = contentPane;
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // Not sure this is needed, but just in case
            }

         }
    }


    class TimerHandler implements ActionListener {
        Calendar tempCalendar = Calendar.getInstance();

        @Override public void actionPerformed(ActionEvent e) {
            Controller.this.c.getCal().add(Calendar.SECOND, 1);
            Controller.this.c.updateTime();
        }//close actionPerformed
    }//close TimerHandler


}
